import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import user from './user'
import order from './order'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[{title:'吃饭',status:false},{title:'睡觉',status:false},{title:'打游戏',status:false}],
    goosList:[{name:'苹果14pro',price:9.9,num:2},{name:'苹果14proMacs',price:10,num:1},{name:'华为mate50',price:2.5,num:1}],
    msg:'今天是专7的第一天',
    taskList:[]
  },
  getters: {
    getPrice(state){
        let total = 0 //放总价的盒子
        state.goosList.forEach(item=>{
           total+=item.price * item.num
        //    total = total +item.price * item.num
        })
        return total
    }
  },
  mutations: {
    add(state,obj){
        state.list.push(obj)
    },
    setList(state,data){
        state.taskList = data
    }
  },
//   存放异步请求
  actions: {
    // 写成对象的形式，是函数参数的解构赋值
    getList({commit}){
    //    请求数据
      axios.get('./list.json').then(res=>{
        console.log(res.data.list);
        // console.log(context);//
        commit('setList',res.data.list)
      })
    }
  },
  modules: {
    user,
    order

    
  }
})
